//
//  MyDocument.m
//  Odeo.app
//
// Created by Dominic Sagolla, from code by Chip Cuccio.
// Copyright Dominic Sagolla 2006. All rights reserved. See the file LICENSE for license information.
//

#import "MyDocument.h"

@implementation MyDocument

- (id)init{
    self = [super init];
    if (self) {
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
}

- (NSString *)windowNibName{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"Player";
}

- (WebView *)webView{
    return webView;
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController{
    [super windowControllerDidLoadNib:aController];
    
    [webView setGroupName:@"Odeo"];
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://odeo.com/play"]]];
    [webView setApplicationNameForUserAgent:@"Safari/417.9"];
    
    [self setFileName:@"Play"];
}


- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request{
    
    id myDocument = [[NSDocumentController sharedDocumentController] openUntitledDocumentOfType:@"OdeoBrowserDocumentType" display:YES];
    [[[myDocument webView] mainFrame] loadRequest:request];
    return [myDocument webView];
}


- (void)webViewShow:(WebView *)sender
{
    id myDocument = [[NSDocumentController sharedDocumentController] documentForWindow:[sender window]];
    [myDocument showWindows];
}

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame{
//    NSLog(@"finished load: first responder is %@ and frame is %@", [[sender window] firstResponder], frame);
    [[sender window] makeFirstResponder:[frame frameView]];
//    NSLog(@"Changed      : first responder is %@ window is %@ key", [[sender window] firstResponder], ([[sender window] isKeyWindow] ? @"" : @"NOT"));
//    NSLog(@"didFinishLoadForFrame");
}

-(NSURLRequest *)webView:(WebView *)sender 
                resource:(id)identifier
         willSendRequest:(NSURLRequest *)request 
        redirectResponse:(NSURLResponse *)redirectResponse 
          fromDataSource:(WebDataSource *)dataSource
{
    // Update the status message
    [self updateResourceStatus];
    return request;
}

- (id)webView:(WebView *)sender 
identifierForInitialRequest:(NSURLRequest *)request fromDataSource:(WebDataSource *)dataSource{
    // Return some object that can be used to identify this resource
    return [NSNumber numberWithInt:resourceCount++];
}


-(void)webView:(WebView *)sender resource:(id)identifier 
didFailLoadingWithError:(NSError *)error 
fromDataSource:(WebDataSource *)dataSource
{
    resourceFailedCount++;
    // Update the status message
    [self updateResourceStatus];
}

-(void)webView:(WebView *)sender 
      resource:(id)identifier 
didFinishLoadingFromDataSource:(WebDataSource *)dataSource
{
    resourceCompletedCount++;
    // Update the status message
    [self updateResourceStatus];    
}

- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame{
    // reset resource status
    resourceFailedCount = 0;
    resourceCount = 0;
    resourceCompletedCount = 0;
    //    NSLog(@"didStartProvisionalLoad");
    [progressIndicator setHidden:NO];
    [progressIndicator setDoubleValue:(double)resourceCompletedCount];
}

- (void)updateResourceStatus{
    //    NSLog(@"resourceCount: %d completed: %d failed: %d", resourceCount, resourceCompletedCount, resourceFailedCount);
    [progressIndicator setMaxValue:(double)resourceCount];
    [progressIndicator setDoubleValue:(double)resourceCompletedCount];
    if((resourceCompletedCount + resourceFailedCount) >= resourceCount){
        [progressIndicator setHidden:YES];
    }else{
        [progressIndicator setHidden:NO];
    }
}
 
@end
